var dialogManager = require("dialogManager");
var config = require("config");
cc.Class({
    extends: cc.Component,

    properties: {
        content_weiyang : {
            default : null,
            type : cc.Node
        },
        petItem_weiyang : {
            default : null,
            type : cc.Prefab
        },
        chouNode : {
            default : null,
            type : cc.Node
        },
        zhuanpanNode : {
            default : null,
            type : cc.Node
        },
        gold:cc.Label,
        img_pet : {
            default : null,
            type : cc.Node
        },
        lab_pet : {
            default : null,
            type : cc.Label
        },
        ani_pet : {
            default : null,
            type : cc.Node
        },
        sprArr :{
            default:[],
            type:[cc.SpriteFrame],
        },
        petItems : {
            default : [],
            type : [cc.Node]
        },
        zhuanpan_a : {
            default : null,
            type : cc.Node
        },
        bg : {
            default : null,
            type : cc.Node
        },
    },
    onLoad () {
        var height = cc.view.getVisibleSize().height;
        this.node.height = height;
        this.bg.height = height/2*1.3;
        config.loadImage(this.bg,"pet_bg","resources/image/");
        this.chouNode.active = false;
        this.gold.string = config.PlayerGold;
        //转盘数据
        this.canTouchZhuanPan = true;
        this.addAngel = 0;
        this._stopAngel = 0;
        this._angelList = [0, 45, 90, 135, 180, 225, 270, 315];
    },
    start () {
        for(var i = 0;i<6;i++){
            this.petItems[i].active = false;
        }
        //当前拥有宠物
        this.petNum = 0;
        this.zhuanpanNode.active = false;
        for(var i=1;i<=6;i++){
            if(config.petInfo["pet"+i].exist){
                this.petNum++;
                this.petItems[i-1].active = true;
            }
        }
        
        if(this.petNum == 0 && config.PlayerGold >= 1000){
            config.CurPetIndex = -1;
            this.zhuanpanNode.active = true;
        }
        //当前选中宠物
        this.curPetIndex = config.CurPetIndex;
        this.chuZhanBtns = [];//出战按钮
        this.petUpAnis = [];//升级动画
        this.petNodeInfos = [];//宠物节点信息
        for(var i = 0;i<6;i++){
            var Item = this.petItems[i];
            var txt_name = Item.getChildByName('name').getComponent(cc.Label);
            var txt_lv = Item.getChildByName('p_lv').getChildByName('LabelAtlas_lv').getComponent(cc.Label);
            var txt_hit = Item.getChildByName('lab_hit').getChildByName('lab_hitNum').getComponent(cc.Label);
            var txt_gold = Item.getChildByName('pet_cell_gb_bg').getChildByName('lab_gold').getComponent(cc.Label);
            var index = i+1;
            var node_btnGo = Item.getChildByName('btn_go_'+index);
            this.chuZhanBtns.push(node_btnGo);
            var node_upAni = Item.getChildByName('petUp_ani');
            node_upAni.active = false;
            this.petUpAnis.push(node_upAni);
            var ItemInfo ={
                'txt_name':txt_name,
                'txt_lv':txt_lv,
                'txt_hit':txt_hit,
                'txt_gold':txt_gold,
                'node_btnGo':node_btnGo,
            }
            this.showPetItemInfo(i+1,ItemInfo);
            this.petNodeInfos.push(ItemInfo);

            if(this.curPetIndex == i){
                node_btnGo.color = new cc.Color(125, 125, 125);
            }
        }
    },
    show(){},
    showPetItemInfo(index,ItemInfo){
        var lv =config.petInfo["pet"+index].lv;
        var hit = index*50*lv;
        var gold = hit*2;
        ItemInfo.txt_lv.string = lv;
        ItemInfo.txt_hit.string = hit;
        ItemInfo.txt_gold.string = gold;
        config.petInfo["pet"+index].hit = hit;
    },
    showUpLv(index,ItemInfo,isChou){
        var Lv_num =config.petInfo["pet"+index].lv;
        var huafei = index*50*Lv_num*2;
        Lv_num++;
        config.petInfo["pet"+index].lv = Lv_num;
        var hit = index*50*Lv_num;
        var gold = hit*2;
        ItemInfo.txt_lv.string = Lv_num;
        ItemInfo.txt_hit.string = hit;
        ItemInfo.txt_gold.string = gold;
        config.petInfo["pet"+index].hit = hit;

        if(!isChou){
            config.PlayerGold = config.PlayerGold - huafei;
            cc.vv.gameDataMgr.saveGold(config.PlayerGold);
            this.gold.string = config.PlayerGold; 
        }
        cc.vv.gameDataMgr.savePetInfo(index);
    },
    showGetPet(index){
        this.chouNode.active = true;
        this.img_pet.getComponent(cc.Sprite).spriteFrame = this.sprArr[index];  
    },
    petUpClick(event){
        cc.vv.audioMgr.playSFX("ui_button");
        var _name = event.target._name;
        var index = _name.replace("btn_lvup_", "");
        index = parseInt(index);
        this.curPetIndex = index-1;
        var Lv_num =config.petInfo["pet"+index].lv;
        var huafei = index*50*Lv_num*2;
        if(config.PlayerGold < huafei){
            if(typeof(wx)=="undefined"){
                var args = {
                    title:"温馨提示",
                    content:"金币不足无法升级！",
                    enterCallback:null,
                    cancelCallback:null,
                }
                dialogManager.showCommonDialog(args);
            }else{
                dialogManager.showNotEnoughGoldDialog();
            }
            return;
        }
        this.showUpLv(index,this.petNodeInfos[index-1]);
        //播放动画
        this.petUpAnis[this.curPetIndex].active = true;
        var anim = this.petUpAnis[this.curPetIndex].getComponent(cc.Animation);
        anim.play('petup_ani');
        anim.on('finished',  this.onFinished, this);
    },
    petUp(index,isChou){
        this.curPetIndex = index-1;
        this.showUpLv(index,this.petNodeInfos[index-1],isChou);
        //播放动画
        this.petUpAnis[index-1].active = true;
        var anim = this.petUpAnis[index-1].getComponent(cc.Animation);
        anim.play('petup_ani');
        anim.on('finished',  this.onFinished, this);
    },
    //动画结束后
    onFinished: function(event) { 
        this.petUpAnis[this.curPetIndex].active = false;
    },
    petGoClick(event){
        cc.vv.audioMgr.playSFX("ui_button");
        var _name = event.target._name;
        var index = _name.replace("btn_go_", "");
        for(var i=0;i<6;i++){
            this.chuZhanBtns[i].color = new cc.Color(255, 255, 255);
        }
        var curIndex = parseInt(index) - 1;
        this.chuZhanBtns[curIndex].color = new cc.Color(125, 125, 125);
        config.CurPetIndex = curIndex;//当前宠物编号
        config.PetBulletHpDrop = config.petInfo["pet"+index].hit; //宠物子弹攻击力
    },
    chouClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        if(config.PlayerGold>1000){
            // var index = Math.floor(Math.random()*6);
            // this.showGetPet(index);
            this.zhuanpanNode.active = true;
        }else{
            var args = {
                title:"温馨提示",
                content:"金币少于1000无法抽宠物！",
                enterCallback:null,
                cancelCallback:null,
            }
            dialogManager.showCommonDialog(args);
        }
    },
    closeClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        this.node.destroy();
    },
    chouNodeAgainClick(){
        cc.vv.audioMgr.playSFX("ui_button");
    },
    chouNodeCloseClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        this.chouNode.active = false;
    },
    zhuanpanNodeCloseClick(){
        if(this.canTouchZhuanPan){
            cc.vv.audioMgr.playSFX("ui_button");
            this.zhuanpanNode.active = false;
        }
    },
    zhuanpanClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        if(config.PlayerGold>1000){
            this.getLuckDrawNum();
        }else{
            if(typeof(wx)=="undefined"){
                var args = {
                    title:"温馨提示",
                    content:"金币少于1000无法抽宠物！",
                    enterCallback:null,
                    cancelCallback:null,
                }
                dialogManager.showCommonDialog(args);
            }else{
                dialogManager.showNotEnoughGoldDialog();
            }
        }
    },
    onTurnStart(index){
        var self = this;
        if(!this.canTouchZhuanPan){
            return;
        }
        this.canTouchZhuanPan = false;
        this._stopAngel = this._angelList[self.angleIndex-1];
        var angelZ = 360*6 + self.addAngel + self._stopAngel;
        var rotate1 = cc.rotateBy(7, angelZ);
        var ease1 = rotate1.easing(cc.easeExponentialOut());
        var callFunc = cc.callFunc(function(){
            self.canTouchZhuanPan = true;
            self.addAngel = -self._stopAngel;
            if(index>self.petNum&&index<7){
                self.petNum++;
            }
            if(index<7){
                if(config.petInfo["pet"+index].exist){
                    console.log("已经存在，等级加一 宠物编号:",index);
                    self.petUp(index,true);
                }else{
                    console.log("抽到新宠物：",index);
                    self.petItems[index-1].active = true;
                    config.petInfo["pet"+index].exist = true;
                    cc.vv.gameDataMgr.savePetInfo(index);
                }
            }
            if(index == 7){
                self.saveGolds(500);
            }else if(index == 8){
                self.saveGolds(9000);
            }
            
        });
        var sequence = cc.sequence(ease1, callFunc);
        self.zhuanpan_a.runAction(sequence);
        this.saveGolds(-1000);
    },
    saveGolds(gold){
        config.PlayerGold = config.PlayerGold + gold;
        cc.vv.gameDataMgr.saveGold(config.PlayerGold);
        this.gold.string = config.PlayerGold;
    },
    //概率抽奖 
    //转盘对应宠物编号 7(500) 8(9000)config.ZhuanPanPets=[0,1,3,7,6,4,5,8,2];
    getLuckDrawNum(){
        var randomIndex = Math.floor(Math.random()*100);
        console.log("randomIndex:",randomIndex);
        var index = 1;
        if(this.petNum == 0){
            index = 1;
        }else if(this.petNum > 0 && this.petNum < 6){
            if(randomIndex<10){
                index = this.petNum+1;
            }else if(randomIndex>=10&&randomIndex<50){
                index = 7;
            }else if(randomIndex==50){
                index = 8;
            }else{
                index = Math.floor(Math.random()*this.petNum)+1;
            }
        }else{
            if(randomIndex>=5&&randomIndex<10){
                index = 7;
            }else if(randomIndex==10){
                index = 8;
            }else{
                index = Math.floor(Math.random()*6)+1;
            }
        }
        this.angleIndex = config.ZhuanPanPets[index];
        this.onTurnStart(index);
    }
});
